package com.example.automanagement.service.serviceImpl;

import com.example.automanagement.dto.OrderDto;
import com.example.automanagement.entity.AutoOrder;
import com.example.automanagement.entity.RepairOrder;
import com.example.automanagement.mapper.OrderMapper;
import com.example.automanagement.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<AutoOrder> getAutoOrder(String userId,String userRole){
        List<AutoOrder> temp = new ArrayList<>();
        switch (userRole) {
            case "综合办" -> temp = orderMapper.getAutoOrder("");
            case "部门管理员" -> {
                String userDepartment = orderMapper.getDepartment(userId);
                temp = orderMapper.getAutoOrderByDepartment(userDepartment);
            }
            case "用车人" -> {
                temp = orderMapper.getAutoOrder(userId);
            }
            default -> {
            }
        }
        return temp;
    }

    @Override
    public AutoOrder getAutoOrderDetail(String autoOrderId) {
        return orderMapper.getAutoOrderDetail(autoOrderId);
    }

    @Override
    public RepairOrder getRepairOrderDetail(String repairOrderId) {
        return orderMapper.getRepairOrderDetail(repairOrderId);
    }

    @Override
    public List<RepairOrder> getFixOrder(String userId,String userRole){
        List<RepairOrder> fixOrder = orderMapper.getFixOrder("");
        List<RepairOrder> temp = new ArrayList<>();
        switch (userRole) {
            case "综合办" -> temp = fixOrder;
            case "驾驶员" -> {
                for (RepairOrder order : fixOrder) {
                    if (order.getUserId().equals(userId)) {
                        temp.add(order);
                    }
                }
            }
            default -> {
            }
        }
        return temp;
    };

    @Override
    public List<OrderDto> getOrder(String userId,String userRole){
        OrderServiceImpl orderService = new OrderServiceImpl();
        List<OrderDto> orderList=new ArrayList<>();
        switch (userRole){
            case "用车人":{
                List<AutoOrder> autoOrders=orderMapper.getAutoOrder(userId);
                orderList = orderService.autoToOrderDto(autoOrders);
                break;
            }
            case "部门管理员":{
                String userDepartment = orderMapper.getDepartment(userId);
                List<AutoOrder> autoOrders=orderMapper.getAutoOrderByDepartment(userDepartment);
                orderList = orderService.autoToOrderDto(autoOrders);
                break;
            }
            case "综合办":{
                List<AutoOrder> autoOrders = orderMapper.getAutoOrder("");
                List<RepairOrder> repairOrders = orderMapper.getFixOrder("");
                List<OrderDto> temp = orderService.autoToOrderDto(autoOrders);
                orderList = orderService.repairToOrderDto(repairOrders);
                orderList.addAll(temp);
                break;
            }
            case "驾驶员":{
                List<RepairOrder> repairOrders=orderMapper.getFixOrder(userId);
                orderList = orderService.repairToOrderDto(repairOrders);
                break;
            }
            default:{
                return orderList;
            }
        }
        Collections.sort(orderList, (o1, o2) -> o2.getSubmitTime().compareTo(o1.getSubmitTime()));
        return orderList;
    };

    public List<OrderDto> autoToOrderDto(List<AutoOrder> autoOrders){
        List<OrderDto> orderDtos = new ArrayList<>();
        for (AutoOrder autoOrder : autoOrders) {
            OrderDto combinedOrder = new OrderDto();
            combinedOrder.setOrderId(autoOrder.getAutoOrderId());
            combinedOrder.setUserName(autoOrder.getUserName());
            combinedOrder.setSubmitTime(autoOrder.getSubmitTime());
            combinedOrder.setOrderStatus(autoOrder.getAutoOrderStatus());
            combinedOrder.setOrderProgress(autoOrder.getAutoOrderProgress());
            combinedOrder.setType("用车工单");
            orderDtos.add(combinedOrder);
        }
        return orderDtos;
    };
    public List<OrderDto> repairToOrderDto(List<RepairOrder> repairOrders){
        List<OrderDto> orderDtos = new ArrayList<>();
        for (RepairOrder repairOrder : repairOrders) {
            OrderDto combinedOrder = new OrderDto();
            combinedOrder.setOrderId(repairOrder.getRepairOrderId());
            combinedOrder.setUserName(repairOrder.getUserName());
            combinedOrder.setSubmitTime(repairOrder.getSubmitTime());
            combinedOrder.setOrderStatus(repairOrder.getRepairOrderStatus());
            combinedOrder.setOrderProgress(repairOrder.getRepairOrderProgress());
            combinedOrder.setType("维修工单");
            orderDtos.add(combinedOrder);
        }
        return orderDtos;
    };
}
